home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 7_10.lha / 7_10 / test5.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  2KB  |  95 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. *ident    "@(#)ctrans:demo/task/shared.C    1.1.1.1" */
  6. include <debug.h>    /* DELETE */
  7. **************************************************************************
  8.         Copyright (c) 1984 AT&T
  9.             All Rights Reserved      
  10.  
  11. THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
  12.  
  13. The copyright notice above does not evidence any       
  14. actual or intended publication of such source code.
  15.  
  16. ****************************************************************************/
  17. include <process.h>
  18.  
  19. * non-trivial process example:
  20. Make a set of SHARED processes
  21. which pass an object round between themselves.
  22. Each process but the last creates the next process.
  23. Each process gets an object from the head of one queue, and puts
  24. the object on the tail of another queue.
  25. Each process quits after getting the object MAX_CYCLES times.
  26. /
  27.  
  28. onst int NTASKS = 1;
  29. onst int MAX_CYCLES = 5;
  30.  
  31. truct pc : process {                // derive a class from process
  32. queue_tail *t;
  33. queue_head *h;
  34. char *n;
  35.        pc(char*nn, queue_tail*nt, queue_head*nh) // process is not intended to be used directly
  36. : (nn)
  37.     t = nt; h = nh; n = nn;
  38. }
  39. long main();
  40. ;
  41.  
  42. ong pc::main()                    // process body serves as
  43.                         // "main" program for process
  44. int cycles;
  45.  
  46.        printf("new pc(%s)\n",n);
  47. if (*n < 'a'+ NTASKS) {
  48.     char*    c = new char[2]; c[0] = *n + 1; c[1] = '\0';
  49.     queue_tail*    qt = new queue_tail;
  50.     pc*    next = new pc(c, t, qt->head());
  51.     next->exec();
  52.     t = qt;
  53. } else {
  54.     printf("%s: here we go\n", n);
  55.     t->put(new process_object);
  56. }
  57.        for (cycles = 0; cycles < MAX_CYCLES; cycles++) {
  58.     process_object* p = h->get();
  59.     printf("process %s\n",n);
  60.     t->put(p);
  61. }
  62. printf("process %s: done.\n", n);
  63. exit(0);    // Always end process constructors with exit or return.
  64.  
  65.  
  66. ain()
  67.  
  68. queue_head* hh = new queue_head;
  69. queue_tail* t = hh->tail();
  70. queue_head* h;
  71. int cycles;
  72.  
  73. printf("main\n");
  74.  
  75. char* n = "a"; // make a one letter process name
  76.  
  77. h = new queue_head;
  78. (new pc(n,t,h))->exec();
  79. printf("main()'s loop\n");
  80. t = h->tail();
  81.        for (cycles = 0; cycles < MAX_CYCLES; cycles++) {
  82.     process_object* p = hh->get();
  83.     printf("main process\n");
  84.     t->put(p);
  85. }
  86. printf("main process: done.\n", n);
  87. main_process()->exit(0);        // main is a process too; it must also
  88.                 // end with resultis (to allow any
  89.                 // remaining processes to run, otherwise
  90.                 // the whole process would exit).
  91.    printf("should not get here\n");
  92.    return 0;
  93.  
  94.